package com.zwj.web;

import com.zwj.bean.Cart;
import com.zwj.bean.Order;
import com.zwj.bean.OrderItem;
import com.zwj.bean.User;
import com.zwj.service.Impl.OrderServiceImpl;
import com.zwj.service.OrderService;
import com.zwj.utils.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zwj
 * @create 2021-05-16-10:21
 */
public class OrderServlet extends BaseServlet{
    private OrderService orderService = new OrderServiceImpl();
    /**
     * 生成订单,由cart.jsp去结账访问请求
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 先获取 Cart 购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        // 获取 Userid
        User loginUser = (User) req.getSession().getAttribute("user");
        if (loginUser == null) {
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
            return;
        }
        Integer userId = loginUser.getId();
        // 调用 orderService.createOrder(Cart,Userid);生成订单
        String orderId = orderService.createOrder(cart, userId);
        // req.setAttribute("orderId", orderId);
        // 请求转发到/pages/cart/checkout.jsp
        // req.getRequestDispatcher("/pages/cart/checkout.jsp").forward(req, resp);
        req.getSession().setAttribute("orderId",orderId);
        resp.sendRedirect(req.getContextPath()+"/pages/cart/checkout.jsp");
    }

    /**
     * 查看我的订单，由login_sucess_menu.jsp的我的订单访问请求
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showMyOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取当前用户id
        User user = (User)req.getSession().getAttribute("user");
        Integer userId = user.getId();
        //查询当前用户id的订单
        List<Order> orders = orderService.showMyOrders(userId);
        //将所有订单存入域中
        req.getSession().setAttribute("orders",orders);
        //请求重定向到order.jsp
        //req.getRequestDispatcher("pages/client/order.jsp").forward(req,resp);
        resp.sendRedirect(req.getContextPath()+"/pages/client/order.jsp");
    }

    /**
     * 查看订单详情，由order.jsp的查看详情请求访问
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showOrderdetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取OrderID
        String orderId = req.getParameter("orderId");
        //查询订单id的所有订单项
        List<OrderItem> orderItems = orderService.showOrderDetail(orderId);
        //将所有订单项存入域中
        req.getSession().setAttribute("orderItems",orderItems);
        //请求转发到orderItems.jsp
        //req.getRequestDispatcher("pages/order/orderItms.jsp").forward(req,resp);
        resp.sendRedirect(req.getContextPath()+"/pages/order/orderItms.jsp");
    }

    /**
     * 查询所有订单,由manager_menu.jsp的订单管理访问请求
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showAllOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询所有订单
        List<Order> managerOrders = orderService.queryAllOrders();
        //存入域中
        req.getSession().setAttribute("managerOrders",managerOrders);
        //请求转发到order.jsp
        //req.getRequestDispatcher("pages/manager/order_manager.jsp").forward(req,resp);
        resp.sendRedirect(req.getContextPath()+"/pages/manager/order_manager.jsp");
    }

    /**
     * 发货,由order_manager.jsp的点击发货访问请求
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void sendOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取OrderID
        String orderId = req.getParameter("orderId");
        //改变订单的status属性
        orderService.sendOrder(orderId);
        //请求转发回来刷新数据
        //req.getRequestDispatcher("pages/manager/order_manager.jsp").forward(req,resp);
        resp.sendRedirect(req.getContextPath()+"/orderServlet?action=showAllOrders");
    }

    /**
     * 由order.jsp的点击签收访问得到
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void receiverOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取OrderID
        String orderId = req.getParameter("orderId");
        //改变订单的status属性
        orderService.receiverOrder(orderId);
        //请求转发回来刷新数据
        //req.getRequestDispatcher("pages/client/order.jsp").forward(req,resp);
        resp.sendRedirect(req.getContextPath()+"/orderServlet?action=showMyOrders");
    }
}
